import { App,h, resolveComponent } from 'vue';
import { createRouter, createWebHashHistory,RouteRecordRaw  } from "vue-router"
import {createRouterGuards} from "./router-guards"
import _ from 'lodash-es'
// 前台管理
import homePageLayout from "@/layout/index.vue"

import dashbord from "@/views/dashbord/index.vue"
import fontUser from "@/views/font/user/index.vue"
// import personal from "@/views/personal/index.vue"
// 后台管理
import backHomeLayout from "@/layout/homeIndex.vue"
import backHome from "@/views/system/home/index.vue"
// import systemUser from ""
// import systemRole from "@/views/system/role/index.vue"
// import systemMenu from "@/views/system/menu/index.vue"
// import systemTemplate from "@/views/system/template/index.vue"
// common
import login from "@/views/login/Login.vue"
import notFound from "@/views/errorPage/NotFound.vue"
// import redirect from "@/views/redirect/index.vue"

// const components = {
// 	'systemUser': () =>import("@/views/system/user/index.vue"),
// 	'systemRole': () =>import("@/views/system/role/index.vue"),
// 	'systemMenu': () =>import("@/views/system/menu/index.vue"),
// 	'systemTemplate': () =>import("@/views/system/template/index.vue"),
// 	'personal': () =>import("@/views/personal/index.vue"),
// 	'homePageLayout': () =>import("@/layout/index.vue"),
// 	'backHomeLayout': () =>import("@/layout/homeIndex.vue")
// }

const routes:Array<RouteRecordRaw> = [
	// {
	// 	path: '/redirect',
	// 	component: backHomeLayout,
	// 	children: [
	// 		{
	// 			path: '/redirect/:path(.*)',
	// 			component: () => redirect
	// 		}
	// 	]
	// },
	{
		path: '/login',
		name: '登录',
		component: () => login
	},
	{
		path: '/404',
		name: 'NotFound',
		component: () => notFound
	},
	{
		path: '/',
		name: 'dashbord',
		//使用import可以路由懒加载，如果不使用，太多组件一起加载会造成白屏
		component: () => homePageLayout,
		redirect: "/dashbord",
		children: [
			{
				path: '/dashbord',
				component: () => dashbord,
				meta: { transition: 'slide-left' },
			}
		]
	},
	{
		path: '/home',
		//使用import可以路由懒加载，如果不使用，太多组件一起加载会造成白屏
		component: () => backHomeLayout,
		redirect:'/home/index',
		children: [
			{
				path: 'index',
				name: 'backHome',
				//使用import可以路由懒加载，如果不使用，太多组件一起加载会造成白屏
				component: () => backHome,
				meta: { transition: 'slide-right' },
			}
		]
	}
]
/**
 * 获取路由
 */
const RouteView = {
    name: "RouteView",
    render: () => h(resolveComponent("router-view"))
}
export const generatorRouter = (routes:Array<Object>) => {
	const routesData = routes.map((routeItem:any)=>{
		const routeInstance:any = {}
		const {menu_path,redirect_path,component,component_name,second_component,children,child_menu_show} = routeItem
		routeInstance["path"] = menu_path
		routeInstance["name"] = component_name
		if(children && children.length > 0){
			if(component != 'RouteView'){
				routeInstance["component"] = ()=> import(`../${component}`) //components[component]
			}else{
				routeInstance["component"] = ()=> RouteView //components[component]
			}
		}else{
			routeInstance["component"] = ()=> import(`../${second_component}`)
		}
		if(redirect_path){
			routeInstance["redirect"] = redirect_path
		}
		if(child_menu_show === 1){ //一级路由 不显示子菜单
			routeInstance["component"] = ()=> import(`../${component}`)
			const child = {
				"path":redirect_path,
				"name": `${component_name}_index`,
				"component": ()=> import(`../${second_component}`) //components[second_component]
			}
			routeInstance["children"] = [child]
			return routeInstance
		}
		if(children && children.length > 0){
			routeInstance["children"] = generatorRouter(children)
		}
		return routeInstance
	})
	return routesData
}
/**
 *  获取菜单列表
 */
export function getMenuListByRoute(routes:Array<Object>,parentKey:String){
	return routes.map((routeItem:any)=>{
		const menuInstance:any = {}
		const {menu_path,redirect_path,menu_name,component_name,children,child_menu_show,icon} = routeItem
		menuInstance["label"] = menu_name
		menuInstance["key"] = component_name
		menuInstance["icon"] = icon?icon:()=>null
		if(parentKey){
			menuInstance["parentKey"] = parentKey
		}
		if(!redirect_path){
			menuInstance["path"] = menu_path
		}
		if(child_menu_show === 1){ //一级路由 不显示子菜单
			menuInstance["path"] = menu_path
			return menuInstance
		}
		if(children && children.length > 0){
			let parentNode = ''
			if(parentKey){
				parentNode = parentKey + ',' + component_name
			}else{
				parentNode = component_name
			}
			menuInstance["children"] = getMenuListByRoute(children,parentNode)
		}
		return menuInstance
	})
}
const router = createRouter({
	history: createWebHashHistory(),
	routes
})
export function setupRouter(app:App){
	app.use(router)
	createRouterGuards(router)
}
export default router